package com.visual.echart.controller;

import com.alibaba.fastjson.JSON;
import com.visual.echart.dao.MarketDataRepository;
import com.visual.echart.model.BaseResp;
import com.visual.echart.model.entity.MarketData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

/**
 * 简介说明:大盘数据
 *
 * @author: biboyuzhou
 * @date: 2025/06/15 16:19:56
 * @version:1.0
 */
@Controller
@RequestMapping("/market")
public class MarketDataController {
    @Autowired
    private MarketDataRepository marketDataRepository;

    public static void main(String[] args) {
        MarketData marketData = new MarketData();
        //marketData.setId(6L);
        marketData.setDate("20250627");
        marketData.setVolume(15411);
        marketData.setRedNum(3227);
        marketData.setGreenNum(1662);
        marketData.setLimitUpNum(54);
        marketData.setLimitDownNum(3);
        marketData.setHighestNum(4);
        marketData.setTemperature(0);
        String json = JSON.toJSONString(marketData);
        System.out.println(json);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>(json, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity("http://localhost:8080/market/save", entity, String.class);

    }

    @PostMapping("/save")
    public BaseResp save(@RequestBody String data){
        BaseResp baseResp = new BaseResp();

        MarketData marketData = JSON.parseObject(data, MarketData.class);
        MarketData result = marketDataRepository.save(marketData);
        if (Objects.nonNull(result)) {
            baseResp.setSuccessData();
        } else {
            baseResp.fillErrorResp(baseResp);
        }

        return baseResp;
    }
}
